/**!
 *
 * 添加举报。
 *
 */

$(function () {
    /** 举报表单的 jQuery 对象 */
    var $reportForm = $("#report_form");
    /** 举报图片名称的 jQuery 对象 */
    var $reportImages = $reportForm.find("input[name='reportImages']");
    /** 上传举报图片的 jQuery 对象 */
    var $uploadReportImagesBtn = $("#upload_report_images_btn");
    /** 显示校验信息的容器 */
    var $validatecontainer = $("#validate_container");
    /** 添加按钮的 jQuery 对象 */
    var $addReportBtn = $reportForm.find("button[name='addReportBtn']");
    /** 举报图片的路径 */
    var reportImages = "";

    // 上传图片
    var $reportImagesUploadifive = $uploadReportImagesBtn.myUploadifive({
        auto: false,
        multi: true,
        width: 80,
        height: 30,
        queueID: "report_queue",
        fileType: "image/*",
        buttonText: "<i class='ace-icon iconfont icon-upload f-s-12'></i>上传图片",
        buttonClass: "btn btn-success btn-xs",
        fileObjName: "file",
        uploadScript: "{0}web/file/upload.json".format(globalBasePath),
        fileSizeLimit: String(50 * 1024),
        onUploadFn: function () {
            $reportImagesUploadifive.uploadifiveData.settings.formData.path = "/upload/report";
        },
        onUploadCompleteFn: function (file, data) {
            var result = data.result;

            reportImages += (result.filePath + "|");
        },
        onQueueCompleteFn: function () {
            $reportImages.val(reportImages.substring(0, (reportImages.length - 1)));

            // 提交表单
            $reportForm.get(0).submit();
        }
    });

    $reportForm.validate({
        rules: {
            reportName: {
                required: true,
                maxlength: 500
            },
            reportContent: {
                required: true,
                maxlength: 2000
            },
            reportSource: {
                required: true
            },
            reportCreatePerson: {
                required: true,
                maxlength: 50
            }
        },
        messages: {
            reportName: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入举报标题</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>举报标题的长度不能大于{0}个字符</span>"
            },
            reportContent: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入举报内容</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>举报内容的长度不能大于{0}个字</span>"
            },
            reportSource: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请选择举报来源</span>"
            },
            reportCreatePerson: {
                required: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>请输入举报人</span>",
                maxlength: "<span class='f-s-12'><i class='iconfont icon-error f-s-12'></i>举报人的长度不能大于{0}个字符</span>"
            }
        },
        errorContainer: $validatecontainer,
        errorLabelContainer: $("ul", $validatecontainer),
        wrapper: "li",
        submitHandler: function (form) {
            $addReportBtn.html("<i class='ace-icon fa fa-spinner fa-spin bigger-150'></i>正在提交…").attr({"disabled": "disabled"});

            if ($.isNotNull($reportImagesUploadifive) && ($reportImagesUploadifive.uploadifiveData.queue.count > 0)) {
                // 开始上传文件
                $uploadReportImagesBtn.uploadifive("upload");
            } else {
                // 提交表单
                $reportForm.get(0).submit();
            }

            return false;
        }
    });
});